#include "delay.h"
#include "sys.h"
#include "led.h"
#include "key.h"
#include "adc.h"
#include "iwdg.h"
#include "pwm.h"
#include "Whole_Motor_Parameters.h"
#include "GPIO.h"
#include "pwr_pvd.h"
#include "my_uart.h"
#include "for_test_message.h"
#include "PWM_control.h"
#include "time_manager.h"
#include "motor_status.h"
#include "BLDC_change.h"
#include "BLDC_speed_control.h"
#include "BLDC_voltage_condition.h"
#include "BLDC_condition_judgement.h"
extern uint32_t adc1_times, time_too_long_cnt;
extern pole_status pole_status_set;
uint32_t Bemf_A_number[45];
uint16_t u16ADC1[16];
uint16_t u16ADC2[16];
uint32_t u16ADC1_16in1_adc[10];
uint32_t u16ADC1_2in1_adc[10];
uint32_t adc_5in1_adc1_1[6];
u16 begin_cnt = 0, end_cnt = 0, temp_cnt1 = 0, temp_cnt2 = 0, temp_cnt3 = 0, temp_cnt4 = 0;
u16 i_high_value = 0, i_low_value = 0;
u16 out_times = 0;
pid_virables pid_voltage_error_controller, pid_current_error_controller, pid_time_set_controller;
extern u16 condition_judgement_calculated, count_for_condition_judgement;
// uint64_t time_running = 0;
void ADC1_IRQHandler(void)
{
    begin_cnt = TIM1->CNT;
    if (adc1_times++ > 10000000)
    {
        adc1_times = 0;
    }
    temp_cnt1 = TIM1->CNT;
    /* Enable ADC Intterupt */
    // ADC1->ADCR |= ADC_IT_EOC; //
    // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    /* Enable HW Trigger */
    // ADC1->ADCR |= ADCR_EXTTRIG_Set; //
    /* Clear ADC Flag of Conversion */
    ADC1->ADSTA |= ADC_IT_EOC; //
                               // ADC_ClearFlag(ADC1, ADC_IT_EOC);
    ADC2->ADSTA |= ADC_IT_EOC; //
                               // ADC_ClearFlag(ADC2, ADC_IT_EOC);
    // ADC_ITConfig(ADC1, ADC_IT_EOC, DISABLE);

    ADC1->ADCR &= 0xFFFFFFFE;
    // ADC_ITConfig(ADC2, ADC_IT_EOC, DISABLE);
    ADC2->ADCR &= 0xFFFFFFFE;
    /* Disable HW Trigger */
    ADC1->ADCR &= ADCR_EXTTRIG_Reset; //
    // ADC_ExternalTrigConvCmd(ADC1, DISABLE); //检查中断点1
    ADC2->ADCR &= ADCR_EXTTRIG_Reset; //
                                      // ADC_ExternalTrigConvCmd(ADC2, DISABLE);
    // u16ADC1[0] = ADC1->ADDR0;
    // u16ADC1[1] = ADC1->ADDR1;
    // u16ADC1[2] = ADC1->ADDR2;
    //  // u16ADC1[3] = ADC1->ADDR3;
    //  u16ADC2[5] = ADC2->ADDR5;   // w
    //  u16ADC2[6] = ADC2->ADDR6;   // u
    //  u16ADC2[10] = ADC2->ADDR10; // v
    //  u16ADC2[11] = ADC2->ADDR11; //
    temp_cnt4 = TIM1->CNT;
    if (!(condition_judgement_calculated >> 6))
    {
        adc_input_gets_and_eval(ADC1->ADDR0, ADC1->ADDR1, ADC1->ADDR2, ADC2->ADDR6, ADC2->ADDR10, ADC2->ADDR5);
        // BLDC_condition_judgement_check_watching();
    }
    else if (!(condition_judgement_calculated >> 7))
    {
        if ((count_for_condition_judgement & 0x01))
        {
            adc_input_gets_and_eval(ADC1->ADDR0, ADC1->ADDR1, ADC1->ADDR2, ADC2->ADDR6, ADC2->ADDR10, ADC2->ADDR5);
        }
    }
    else
    {
        if (!(count_for_condition_judgement & 0x03))
        {
            adc_input_gets_and_eval(ADC1->ADDR0, ADC1->ADDR1, ADC1->ADDR2, ADC2->ADDR6, ADC2->ADDR10, ADC2->ADDR5);
        }
    }

    temp_cnt2 = TIM1->CNT;
    BLDC_condition_judgement_check_watching();
    temp_cnt3 = TIM1->CNT;

    // adc_flag_set_and_check_status();
    // ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
    ADC1->ADCR |= 0x00000001;
    // ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
    ADC2->ADCR |= 0x00000001;
    // ADC_ExternalTrigConvCmd(ADC1, ENABLE);
    ADC1->ADCR |= ADCR_EXTTRIG_Set;
    // ADC1_Channel_Setup_to_2Phase_Current_Only(); // change ADC1 channels to phase current measurement
    // ADC_ExternalTrigConvCmd(ADC2, ENABLE);
    ADC2->ADCR |= ADCR_EXTTRIG_Set;
    // ADC2_Channel_Setup_to_2Phase_Current_Only(); // change ADC2 channels to phase current measurement
    end_cnt = TIM1->CNT;
    if (end_cnt > 1000)
    {
        time_too_long_cnt++;
        out_times = end_cnt;
    }
}

int fputc(int ch, FILE *f)
{
    while ((UART1->CSR & UART_IT_TXIEN) == 0)
        ; //循环发送,直到发送完毕
    UART1->TDR = (ch & (uint16_t)0x00FF);
    return ch;
}
int main(void)
{

    systick_init();
    LED_Init();
    _my_uart_init();
    delay_ms(300);
    ADC1_Initial();
    ADC1_Channel_Setup__BEMF_ABC();
    ADC2_Initial();
    ADC2_Channel_Setup_to_2Phase_Current();

    TIM1_PWM_Init(PWM_PERIOD, 0, DEAD_TIME_SETUP); // PWM initialize
    // TIM3_PWM_Init(PWM_PERIOD, 0);
    //  motor_status_set(0, 2, 4);
    //  BLDC_motor_modulation_value_set(60);
    TIM_Cmd(TIM1, ENABLE); // Must Enable TIM1, TIM3 Simultaneously
    Pull_Down_all_PWM();
    redefine_all_PWM_Pin();
    SWITCH_LOW_PIN_TO_SD_OUTPUT();
    LOW_PIN_SD_TURN_ON();
    SWITCH_LOW_PIN_TO_SD_float();
    set_motor_start();
    set_limit_reserving_time(&pid_time_set_controller, 0.0001, 0.05, 0.0, PWM_FREQUENCY / 10 / 12); //设置过零时间PID并设置最低值
    set_pid_params(&pid_voltage_error_controller, 0.00001, 0.00005, 0.00, 0, 25);                   //设置压差转速PID并设置最低值
    set_pid_params(&pid_current_error_controller, 0.0001, 0.002, 0.00, 60, 240);                    //设置电流升压电压环
    set_current_target_for_torque(250, 300);
    //   params_set_init();
    //普通：0.001, 0.002, 0.0001
    // Pull_Down_all_PWM();
    while (1)
    {
        System_Time_Management();
    }
}

void MX_ADC1_Init(void)
{

    /* USER CODE BEGIN ADC1_Init 0 */

    /* USER CODE END ADC1_Init 0 */

    ADC_ChannelConfTypeDef sConfig = {0};

    /* USER CODE BEGIN ADC1_Init 1 */

    /* USER CODE END ADC1_Init 1 */

    /** Common config
     */
    hadc1.Instance = ADC1;
    hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
    hadc1.Init.ContinuousConvMode = ENABLE;
    hadc1.Init.DiscontinuousConvMode = DISABLE;
    hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
    hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    hadc1.Init.NbrOfConversion = 8;
    if (HAL_ADC_Init(&hadc1) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_1;
    sConfig.Rank = ADC_REGULAR_RANK_1;
    sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_2;
    sConfig.Rank = ADC_REGULAR_RANK_2;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_3;
    sConfig.Rank = ADC_REGULAR_RANK_3;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_4;
    sConfig.Rank = ADC_REGULAR_RANK_4;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_5;
    sConfig.Rank = ADC_REGULAR_RANK_5;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_6;
    sConfig.Rank = ADC_REGULAR_RANK_6;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_7;
    sConfig.Rank = ADC_REGULAR_RANK_7;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Regular Channel
     */
    sConfig.Channel = ADC_CHANNEL_8;
    sConfig.Rank = ADC_REGULAR_RANK_8;
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN ADC1_Init 2 */

    /* USER CODE END ADC1_Init 2 */
}
